package com.easycarebn.main.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.timbacsi.main.R;

public class FragmentAbout extends Fragment {

    private View mView;
    private static FragmentAbout fragment = null;
    private IResult callIResult;
    private static String mTitle;
    private ProgressBar loadding;
    private WebView wbContent;

    public static FragmentAbout newInstance() {
        if (fragment == null)
            fragment = new FragmentAbout();

        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof IResult) {
            callIResult = (IResult) activity;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_promotions, null);

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadding = (ProgressBar) mView.findViewById(R.id.loadding);
        callIResult.onDisplay(Utils.TASK_CHILD, "");
        
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

        wbContent = (WebView) mView.findViewById(R.id.loadcontent);
        settingWebview(wbContent);
        wbContent.loadUrl(ServerController.BASE_URL+"about");
        
       
    }

    private void settingWebview(WebView webview_fbview) {
        webview_fbview.getSettings().setJavaScriptEnabled(true);
        webview_fbview.setLongClickable(false);

        webview_fbview.getSettings().setLoadWithOverviewMode(true);
        webview_fbview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        webview_fbview.getSettings().setUseWideViewPort(true);
        webview_fbview.requestFocus(View.FOCUS_DOWN);
        webview_fbview.setPadding(0, 0, 0, 0);
        webview_fbview.setWebViewClient(new webViewClient());
        webview_fbview.setWebChromeClient(new webChromeClient());
        webview_fbview.setInitialScale(100);
        webview_fbview.clearCache(true);
        webview_fbview.clearHistory();
        webview_fbview.getSettings().setDefaultFontSize(18);
        webview_fbview.addJavascriptInterface(new JavaScriptInterface(getActivity()), "Android");
        webview_fbview.setVisibility(View.VISIBLE);

        webview_fbview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && loadding.getVisibility() == ProgressBar.GONE) {
                    loadding.setVisibility(ProgressBar.VISIBLE);
                }
                // Pbar.setProgress(progress);
                if (progress == 100) {
                    loadding.setVisibility(ProgressBar.GONE);
                }
            }
        });
    }

    public class JavaScriptInterface {
        Context mContext;

        // Instantiate the interface and set the context
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        // using Javascript to call the finish activity
        public void closeMyActivity() {
            // finish();
        }
    }

    private String getHtmlLink(String content) {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = (size.x / 10) * 9;

        String linkhtml = "<html>" + "<meta name='viewport',content=width=device-width', initial-scale=1, maximum-scale=1>"
                + "<head>" + "<style type='text/css'>" + "img { max-width: " + width + "px!important; width: " + width
                + "px!important; height: auto!important; }" + "p { text-align: justify; width: " + width + "px; }"
                + "@font-face {" + "font-family: MyFont;" + "src: url('file:///android_asset/fonts/Roboto-Light.ttf')}"

                + " </style>" + "</head>"

                + "<body style='margin:0 ; padding: 0;  font-family: MyFont!important;'>"
                + "<div style='padding:20px!important; width=" + width
                + "px; margin: 0 auto;  font-size: 30px!important;line-height:40px'>" + content + "</div>" + "</body></html>";
        Log.d("linkhtml", linkhtml);
        return linkhtml;
    }

    private class webChromeClient extends WebChromeClient {

        // display alert message in Web View
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            new android.app.AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
            result.confirm();
            return true;
        }
    }

    private class webViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // loadding.setVisibility(View.GONE);
            wbContent.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            int pos = url.indexOf("code=");
            loadding.setVisibility(View.GONE);

            super.onPageFinished(view, url);
        }
    }
}
